package com.personal.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.personal.mapper.*;
import com.personal.pojo.*;
import com.personal.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AdminSeviceImp implements AdminService {
    @Autowired
    private TAdminMapper adminMapper;
    @Autowired
    private TMemberMapper memberMapper;
    @Autowired
    private TCoachMapper coachMapper;
    @Autowired
    private TCardMapper cardMapper;
    @Autowired
    private TCourseMapper courseMapper;
    @Autowired
    private TClassroomMapper classroomMapper;
    @Autowired
    private TApplyMapper applyMapper;
    @Autowired
    private TEquipmentMapper equipmentMapper;
    @Override
    public TAdmin login(String username, String password) {
        return adminMapper.selectByNamePass(username,password);
    }

//1.会员管理：

    //1.1会员列表，显示所有会员卡未到期的会员
    @Override
    public List AllMem() {
        return memberMapper.selectAll();
    }
    //会员信息分页显示
    @Override
    public PageInfo splitpage1(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List list = memberMapper.selectAll();
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }
    //多条件查询
    @Override
    public List adminFindMem(String mindate, String maxdate, String memname) {
        return memberMapper.adminSelectMem(mindate,maxdate,memname);
    }

    //修改前显示会员原来信息
    @Override
    public TMember editShow(Integer memid) {
        return memberMapper.selectByPrimaryKey(memid);
    }

    //添加新会员
    @Override
    public int addNewMem(TMember member) {
        return memberMapper.register(member);
    }

    @Override
    public PageInfo splitpage2(int pageNum, int pageSize,String mindate, String maxdate, String memname) {
        PageHelper.startPage(pageNum,pageSize);
        List list = memberMapper.adminSelectMem(mindate,maxdate,memname);
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    //1.2删除列表，显示删除掉的会员信息,分页显示
    @Override
    public PageInfo CardEnd(int pageNum,int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List list = memberMapper.selectCardEnd();
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    //1.2删除会员信息，将状态置0
    @Override
    public int editByStates0(Integer memid) {
        TMember member = new TMember();
        member.setMemid(memid);
        member.setMemstates("0");
        return memberMapper.updateByPrimaryKeySelective(member);
    }

    @Override
    public PageInfo splitpage3(int pageNum, int pageSize, String memname) {
        PageHelper.startPage(pageNum,pageSize);
        List list = memberMapper.adminSelectDeMem(memname);
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    //1.2恢复删除会员信息，将状态置1
    @Override
    public int editByStates1(Integer memid) {
        TMember member = new TMember();
        member.setMemid(memid);
        member.setMemstates("1");
        return memberMapper.updateByPrimaryKeySelective(member);
    }
    //1.3彻底删除会员信息
    @Override
    public int deleteByid(Integer memid) {
        return memberMapper.deleteByPrimaryKey(memid);
    }

    //1.4修改会员信息
    @Override
    public int editMem(TMember member) {
        member.setMemstates("1");
        member.setMemrole(3);
        return memberMapper.updateByPrimaryKeySelective(member);
    }
//2.教练操作:
    //查询所有在职教练，分页显示
    @Override
    public PageInfo splitCoach(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List list = coachMapper.selectByExample();
        PageInfo info = new PageInfo(list);
        return info;
    }
    //查询所有离职教练，分页显示
    @Override
    public PageInfo splitDeCoach(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List list = coachMapper.selectByExample2();
        PageInfo info = new PageInfo(list);
        return info;
    }
    @Override
    public int deCoSt0(Integer coachid) {
        TCoach coach = new TCoach();
        coach.setCoachid(coachid);
        coach.setCoachstates("0");
        int row = coachMapper.updateByPrimaryKeySelective(coach);
        return row;
    }

    @Override
    public int deCoSt1(Integer coachid) {
        TCoach coach = new TCoach();
        coach.setCoachid(coachid);
        coach.setCoachstates("1");
        int row = coachMapper.updateByPrimaryKeySelective(coach);
        return row;
    }

    @Override
    public int deleteCo(Integer coachid) {
        int row = coachMapper.deleteByPrimaryKey(coachid);
        return row;
    }

    @Override
    public TCoach editShowCo(Integer coachid) {
        TCoach coach = coachMapper.selectByPrimaryKey(coachid);
        return coach;
    }

    @Override
    public int editCoach(TCoach coach) {
        int row = coachMapper.updateByPrimaryKeySelective(coach);
        return row;
    }
//  批量删除会员
    @Override
    public int delBatch(String memids) {
        String[] strs = memids.split(",");
        Integer[] ids = new Integer[strs.length];
        for (int i=0;i<strs.length;i++) {
            ids[i] = Integer.valueOf(strs[i]);
        }
        return memberMapper.delBatch(ids);
    }
    @Override
    public int delBatch2(String memids) {
        String[] strs = memids.split(",");
        Integer[] ids = new Integer[strs.length];
        for (int i=0;i<strs.length;i++) {
            ids[i] = Integer.valueOf(strs[i]);
        }
        return memberMapper.delBatch2(ids);
    }

//    批量删除教练
    @Override
    public int delBatchCoach(String memids) {
        String[] strs = memids.split(",");
        Integer[] ids = new Integer[strs.length];
        for (int i=0;i<strs.length;i++) {
            ids[i] = Integer.valueOf(strs[i]);
        }
        return coachMapper.delBatch(ids);
    }
    @Override
    public int delBatchCoach1(String memids) {
        String[] strs = memids.split(",");
        Integer[] ids = new Integer[strs.length];
        for (int i=0;i<strs.length;i++) {
            ids[i] = Integer.valueOf(strs[i]);
        }
        return coachMapper.delBatch2(ids);
    }
//    批量删除课程
    @Override
    public int delCourseBatch0(String courseids) {
        //暂时
        String[] strs = courseids.split(",");
        Integer[] ids = new Integer[strs.length];
        for (int i=0;i<strs.length;i++) {
            ids[i] = Integer.valueOf(strs[i]);
        }
        return courseMapper.delCourseBatch0(ids);
    }
    @Override
    public int delCourseBatch(String courseids) {
        //彻底
        String[] strs = courseids.split(",");
        Integer[] ids = new Integer[strs.length];
        for (int i=0;i<strs.length;i++) {
            ids[i] = Integer.valueOf(strs[i]);
        }
        return courseMapper.delCourseBatch1(ids);
    }

    @Override
    public int addNewCo(TCoach coach) {
        int row = coachMapper.insertSelective(coach);
        return row;
    }

//3.会员卡操作
    @Override
    public List findAllCard() {
        TCardExample example = new TCardExample();
        List list = cardMapper.selectByExample(example);
        return list;
    }

    @Override
    public int addCard(TCard card) {
        int row = cardMapper.insertSelective(card);
        return row;
    }

    @Override
    public int delCardByid(Integer cardid) {
        int row = cardMapper.deleteById(cardid);
        return row;
    }

    @Override
    public int editCard(TCard card) {
        int row = cardMapper.updateByCard(card);
        return row;
    }

    @Override
    public TCard findById(Integer id) {
        TCard card = cardMapper.selectById(id);
        return card;
    }

    @Override
    public List findByName(String cardName) {
        List card = cardMapper.selectByName(cardName);

        return card;
    }
//4.课程管理
    @Override
    public PageInfo courseSplit(int pageNum,int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List list = courseMapper.selectNotEnd();
        PageInfo info = new PageInfo(list);
        return info;
    }

    @Override
    public int delCourse(Integer courseid) {
        return courseMapper.updateState0(courseid);
    }
    @Override
    public int delCourse0(Integer courseid) {
        return courseMapper.deleteByPrimaryKey(courseid);
    }
    @Override
    public int delCourse1(Integer courseid) {
        return courseMapper.updateState1(courseid);
    }
    @Override
    public List allCoach() {
        return coachMapper.selectByExample();
    }

    @Override
    public TCourse selectCourseById(Integer courseid) {
        return courseMapper.selectByPrimaryKey(courseid);
    }

    @Override
    public List allClassroom() {
        return classroomMapper.selectByExample();
    }

    @Override
    public int updateCourse(TCourse course) {
        return courseMapper.updateByPrimaryKeySelective(course);
    }

    @Override
    public PageInfo findCourse(int pageNum, int pageSize, String courseweek, String classroomid, String coursename) {
        PageHelper.startPage(pageNum,pageSize);
        Integer weekid = 0;
        Integer roomid = 0;
        if (!courseweek.isEmpty()){
            weekid = Integer.valueOf(courseweek);
        }
        if (!classroomid.isEmpty()){
            roomid = Integer.valueOf(classroomid);
        }
        List list = courseMapper.selectByWCC(weekid,roomid,coursename);
        PageInfo info = new PageInfo(list);
        return info;
    }

    @Override
    public int addCourse(TCourse course) {
        return courseMapper.insertSelective(course);
    }

    @Override
    public PageInfo deCourseSplit(int pageNum,int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List list = courseMapper.selectCourseEnd();
        PageInfo info = new PageInfo(list);
        return info;
    }

    @Override
    public PageInfo findCourseName(int pageNum,int pageSize,String coursename) {
        PageHelper.startPage(pageNum,pageSize);
        List list = courseMapper.selectName(coursename);
        PageInfo info = new PageInfo(list);
        return info;
    }

    //课程报名
    @Override
    public PageInfo allApply(int pageNum,int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List list = applyMapper.selectAll();
        PageInfo info = new PageInfo(list);
        return info;
    }

    @Override
    public int delApply0(Integer applyid) {
        return applyMapper.deleteBySt0(applyid);
    }

    @Override
    public TApply editApplyShow(Integer applyid) {
        return applyMapper.selectByPrimaryKey(applyid);
    }

    @Override
    public PageInfo findApplyCluase(int pageNum,int pageSize,String memname, String coursename) {
        PageHelper.startPage(pageNum,pageSize);
        List list = applyMapper.selectByClause(memname,coursename);
        PageInfo info = new PageInfo(list);
        return info;
    }

    @Override
    public boolean checkAdd(String memname, String coursename) {
        List list = applyMapper.selectByClause(memname,coursename);
        if (list.size()==0){
            return true;
        }
        return false;
    }

    @Override
    public int addApply(String memname, String coursename) {
        return applyMapper.insertNew(memname,coursename);
    }

    @Override
    public int countApplyMem(Integer courseid) {
        return applyMapper.countMem(courseid);
    }

    //教室管理
    @Override
    public PageInfo allClassroom(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List list = classroomMapper.selectByExample();
        PageInfo info = new PageInfo(list);
        return info;
    }

    @Override
    public int delClassroom(Integer classroomid) {
        return classroomMapper.deleteByPrimaryKey(classroomid);
    }

    @Override
    public TClassroom classroomShow(Integer classroomid) {
        return classroomMapper.selectByPrimaryKey(classroomid);
    }

    @Override
    public int editClassroom(TClassroom classroom) {
        return classroomMapper.updateByPrimaryKeySelective(classroom);
    }

    @Override
    public int addClassroom(TClassroom classroom) {
        return classroomMapper.insert(classroom);
    }

    @Override
    public PageInfo findClassroom(int pageNum,int pageSize,String classroomname) {
        PageHelper.startPage(pageNum,pageSize);
        List list = classroomMapper.findClassroom(classroomname);
        PageInfo info = new PageInfo(list);
        return info;
    }
//  器材管理
    @Override
    public PageInfo allEqu(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List list = equipmentMapper.selectAll();
        PageInfo info = new PageInfo(list);
        return info;
    }

    @Override
    public PageInfo findEqu(int pageNum, int pageSize, String buyDate, String equname) {
        PageHelper.startPage(pageNum,pageSize);
        List list = equipmentMapper.selectByChuase(buyDate,equname);
        PageInfo info = new PageInfo(list);
        return info;
    }

    @Override
    public TEquipment equShow(Integer equid) {
        return equipmentMapper.selectByPrimaryKey(equid);
    }

    @Override
    public int editEqu(TEquipment equipment) {
        return equipmentMapper.updateByPrimaryKeySelective(equipment);
    }

    @Override
    public int addEqu(TEquipment equipment) {
        return equipmentMapper.insertSelective(equipment);
    }

    @Override
    public int deEqu0(Integer equipid) {
        return equipmentMapper.delSt0(equipid);
    }

    @Override
    public int delEqu1(Integer equipid) {
        return equipmentMapper.deleteByPrimaryKey(equipid);
    }

    @Override
    public PageInfo allDeEqu(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List list = equipmentMapper.selectAllDe();
        PageInfo info = new PageInfo(list);
        return info;
    }

    @Override
    public int reEqu(Integer equipid) {
        return equipmentMapper.delSt1(equipid);
    }
}
